package gui.panels;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import events.ResizeEvent;

/**
 * @author Damien Mabin
 */

class ScrollPanel extends Sprite
{
	private var m_ScrollArea 		: Sprite;
	private var m_ScrollBar 		: Sprite;
	private var m_ScrollButton 		: Sprite;
	
	private var m_Elements			: Array<DisplayObject>;
	
	private var m_Style 			: { areaColor : UInt, barColor : UInt, buttonColor : UInt, barWidth : Float };
	private var m_AskedSize 		: Point;
	
	// Stock l'endroit que l'utilisateur est entrain de visionner dans le scroll panel.
	private var m_ViewPoint			: Float;
	private var m_DragPosition		: Point;
	
	public function new(f_Width : Float = 100.0, f_Height:Float = 100.0, ?f_Style:{ areaColor : UInt, barColor : UInt, buttonColor : UInt, barWidth : Float }) 
	{
		super();
		m_AskedSize = new Point();
		m_DragPosition = new Point();
		m_AskedSize.x = f_Width;
		m_AskedSize.y = f_Height;
		m_ViewPoint = 0;
		
		if (f_Style == null)
		{
			m_Style = { areaColor : 0xFFFFFF, barColor : 0x00FFFF, buttonColor : 0x0000FF, barWidth : 15.0 };
		}
		else
		{
			m_Style = f_Style;
		}
		
		m_ScrollArea = new Sprite();
		m_ScrollBar = new Sprite();
		m_ScrollButton = new Sprite();
		m_Elements = new Array<DisplayObject>();
		
		m_ScrollArea.graphics.beginFill(m_Style.areaColor);
		m_ScrollArea.graphics.drawRect(0,  0, m_AskedSize.x - m_Style.barWidth, m_AskedSize.y);
		m_ScrollArea.graphics.endFill();
		
		m_ScrollBar.graphics.beginFill(m_Style.barColor);
		m_ScrollBar.graphics.drawRect(0,  0, 15, m_AskedSize.y);
		m_ScrollBar.graphics.endFill();
		
		m_ScrollButton.graphics.beginFill(m_Style.buttonColor);
		m_ScrollButton.graphics.drawRect(0,  0, 15, m_AskedSize.y);
		m_ScrollButton.graphics.endFill();
		
		this.addChild(m_ScrollArea);
		this.addChild(m_ScrollBar);
		this.addChild(m_ScrollButton);
		
		m_ScrollButton.addEventListener(MouseEvent.MOUSE_DOWN, OnMouseEvent);
		this.addEventListener(ResizeEvent.EVENT, OnResizeEvent);
		OnResizeEvent(new ResizeEvent(m_AskedSize.x, m_AskedSize.y));
	}
	
	public function GetElements():Array<DisplayObject>
	{
		return m_Elements;
	}
	
	private function OnMouseEvent(f_Evt : MouseEvent) : Void
	{
		switch(f_Evt.type)
		{
			case MouseEvent.MOUSE_DOWN :
			{
				if (f_Evt.currentTarget == m_ScrollButton)
				{
					m_ScrollButton.addEventListener(MouseEvent.MOUSE_MOVE, OnMouseEvent);
					m_ScrollButton.addEventListener(MouseEvent.MOUSE_UP, OnMouseEvent);
					m_ScrollButton.addEventListener(MouseEvent.MOUSE_OUT, OnMouseEvent);
					m_DragPosition.x = f_Evt.stageX;
					m_DragPosition.y = f_Evt.stageY;
				}
			}
			case MouseEvent.MOUSE_MOVE :
			{
				if (f_Evt.currentTarget == m_ScrollButton)
				{
					var tempoPos : Point = new Point();
					tempoPos.x = f_Evt.stageX;
					tempoPos.y = f_Evt.stageY;
					m_ViewPoint += m_DragPosition.y - tempoPos.y;
					m_DragPosition = tempoPos;
				}
				OnResizeEvent(new ResizeEvent(m_AskedSize.x, m_AskedSize.y));
			}
			case MouseEvent.MOUSE_UP :
			{
				m_ScrollButton.removeEventListener(MouseEvent.MOUSE_MOVE, OnMouseEvent);
				m_ScrollButton.removeEventListener(MouseEvent.MOUSE_OUT, OnMouseEvent);
			}
			case MouseEvent.MOUSE_OUT :
			{
				m_ScrollButton.removeEventListener(MouseEvent.MOUSE_MOVE, OnMouseEvent);
				m_ScrollButton.removeEventListener(MouseEvent.MOUSE_OUT, OnMouseEvent);
			}
		}
	}
	
	public function AddElement(f_Elt:DisplayObject):Void
	{
		m_Elements.push(f_Elt);
		m_ScrollArea.addChild(f_Elt);
		OnResizeEvent(new ResizeEvent(m_AskedSize.x, m_AskedSize.y));
	}
	
	private function OnResizeEvent(f_Evt:ResizeEvent):Void
	{
		m_AskedSize.x = f_Evt.m_Width;
		m_AskedSize.y = f_Evt.m_Height;
		
		m_ScrollBar.x = f_Evt.m_Width - m_Style.barWidth;
		m_ScrollBar.y = 0;
		m_ScrollBar.graphics.clear();
		m_ScrollBar.graphics.beginFill(m_Style.barColor);
		m_ScrollBar.graphics.drawRect(0,  0, 15, f_Evt.m_Height);
		m_ScrollBar.graphics.endFill();
		
		var verticalOffset : Float = 0;
		for (i in 0 ... m_Elements.length)
		{
			m_Elements[i].y = verticalOffset;
			//m_Elements[i].width = f_Evt.m_Width - m_Style.barWidth;
			verticalOffset += m_Elements[i].height;
		}
		
		if (-m_ViewPoint < 0)
		{
			m_ViewPoint = 0;
		}
		
		m_ScrollArea.x = 0;
		m_ScrollArea.y = 0;
		m_ScrollArea.graphics.clear();
		m_ScrollArea.graphics.beginFill(m_Style.areaColor);
		m_ScrollArea.graphics.drawRect(0,  -m_ViewPoint * (verticalOffset/f_Evt.m_Height), f_Evt.m_Width - m_Style.barWidth, f_Evt.m_Height);
		m_ScrollArea.graphics.endFill();
		m_ScrollArea.scrollRect = new Rectangle(0,  -m_ViewPoint * (verticalOffset/f_Evt.m_Height), f_Evt.m_Width - m_Style.barWidth, f_Evt.m_Height);
		
		m_ScrollButton.graphics.clear();
		if (verticalOffset > f_Evt.m_Height)
		{
			m_ScrollButton.x = f_Evt.m_Width - m_Style.barWidth;
			m_ScrollButton.y = -m_ViewPoint ;
			m_ScrollButton.graphics.beginFill(m_Style.buttonColor);
			m_ScrollButton.graphics.drawRect(0,  0, 15, f_Evt.m_Height * (f_Evt.m_Height/verticalOffset));
			m_ScrollButton.graphics.endFill();
		}
		else
		{
			m_ScrollButton.x = f_Evt.m_Width - m_Style.barWidth;
			m_ScrollButton.y = 0;
			m_ScrollButton.graphics.beginFill(m_Style.buttonColor);
			m_ScrollButton.graphics.drawRect(0,  0, 15, f_Evt.m_Height);
			m_ScrollButton.graphics.endFill();
		}
	}
}